# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-luaxml
pkgver=2.0.0
_pkgver=$pkgver-1
pkgrel=0
pkgdesc="A minimal set of functions for the processing of XML data in Lua"
url="https://github.com/n1tehawk/LuaXML"
arch="all"
license="MIT"
replaces="lua-xml"

source="$pkgname-$pkgver.tar.gz::https://github.com/n1tehawk/LuaXML/archive/7cd4a7ab5db85222edb4c955d53e5674afd170b6.tar.gz
	0001-XML-attributes.patch"
builddir="$srcdir/LuaXML-7cd4a7ab5db85222edb4c955d53e5674afd170b6"

_luaversions="5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev lua-unit"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v-unit"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		make -C "$builddir-$lver" \
			CFLAGS="$CFLAGS -fPIC" \
			LFLAGS="$LDFLAGS -fPIC -shared" \
			INCDIR="$(pkg-config --cflags lua$lver)" \
			LIBS=""
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing on Lua $lver"

		cd "$builddir-$lver"
		lua$lver test.lua
		lua$lver unittest.lua
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"
	replaces="lua$lver-xml"  # for backward compatibility
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/luaxml/$_pkgver"

	cd "$builddir-$lver"

	install -m 755 -D LuaXML_lib.so \
		"$subpkgdir"/usr/lib/lua/$lver/LuaXML_lib.so
	install -m 755 -D LuaXML.lua \
		"$subpkgdir"/usr/share/lua/$lver/LuaXML.lua
	# For backward compatibility, provide both LuaXML.lua and LuaXml.lua
	install -m 755 -D LuaXML.lua \
		"$subpkgdir"/usr/share/lua/$lver/LuaXml.lua

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
584f8aa2c78411bc00d6f11973929c3629a401c312cf10382eb116f18b8d1b267fe2a3f99d7db7a1c8a20f5c963203e760263cccf5e949714a8b58e9412bb661  lua-luaxml-2.0.0.tar.gz
64305a006b36aee1bb8dfcba97b7bff33aa2bcac9746ac39a0e18cbeedecfb1e6f249afd6b8c3dd28b88dd2eccee983f5c53e59db79506d1e72622fb7673d686  0001-XML-attributes.patch
"
